<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
  <head>
    
    <title>权限列表</title>
    
	<meta name="pragma" content="no-cache">
	<meta name="cache-control" content="no-cache">
	<meta name="expires" content="0">    

      <div th:replace="~{commons/script::script}"></div>

  </head>
  
  <body >
   	<table id="roleList" class="easyui-datagrid"></table>
    <div id="toolbar">
    	<a href="javascript:void(0);" onclick="return add('/system/toAddFunction')" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">新建</a>
    	<a href="javascript:void(0);" onclick="return del('/system/deleteFunction')" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true">删除</a>
    	<a href="javascript:void(0);" onclick="return edit('/system/toUpdateFunction');" id="editBtn" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">修改</a>

    </div>
    <script type="text/javascript">

    	//修改按钮事件处理函数
    	function edit(url){
    		var rows = $("#roleList").datagrid("getChecked");
    		if(!rows.length){
    			$.messager.alert("警告","请选择要修改的数据");
    			return false;
    		}
    		if(rows.length>1){
    			$.messager.alert("警告","请选择单条数据进行修改");
    			return false;
    		}
    		var userId = rows[0].id;
    		parent.openTopWindow({
    				width:700,
    				height:500,
    				title:"修改角色",
    				"url":url+"?id="+userId,
    				close:function(){
    					$("#roleList").datagrid("reload");
    				}
    				
    		});
    	}
    	
    	//添加按钮事件处理函数
        function add(url){
        	parent.openTopWindow({
    				width:700,
    				height:500,
    				title:"添加角色",
    				"url": url,
    				close:function(){
    					$("#roleList").datagrid("reload");
    				}
    			});
        }
        //删除按钮事件处理函数
        function del(url){
        	//获取到选中的一行数据
    			var rows = $("#roleList").datagrid("getChecked");
    			
    			if(!rows.length){
    				$.messager.alert("警告","请选择要删除的数据");
    				return;
    			}
    			
    			$.messager.confirm("警告","数据删除后无法恢复，是否确认删除",function(b){
    				if(b){
    					var ids = new Array();
    					$.each(rows,function(index,obj){
    						ids.push(obj.id);
    					});
    					//将数组中元素使用,分割拼接为一个字符串
    					ids = ids.join(",");
    					$.post(url,
    						{
    							"ids" : ids
    						},
    						function(data){
    							if(data && data.success){
    								$.messager.alert("提示",data.message);
    								$("#roleList").datagrid("reload");
    								return;
    							}
    							$.messager.alert("警告",data.message);
    					},"json");
    				}
    			});
        }
    	//加载角色数据
    	$(function(){
    		$("#roleList").datagrid({
    			url : "/system/loadAllFunction",
    			pagination : true,
    			toolbar : "#toolbar",
    			fitColumns : true,
    			idField : "id",
    			rownumbers : true,
    			//singleSelect:true,
    			columns : [
                    [
    				{field:"id",title:"选择",checkbox:true},
    				{field:"funcName",title:"权限名称",sortable:true,width:10},
                    {field:"funcType",title:"权限类型",width:5,formatter:function(value,rowData,index){
                                if(value == 1){
                                    return "菜单";
                                }else if(value == 0){
                                    return "按钮";
                                }else{
                                    return "其他";
                                }
                            }},
    				{field:"funcURL",title:"权限URL",width:15},
    				{field:"funcNote",title:"权限描述",width:10},
    				{field:"createBy",title:"创建者",width:15},
    				{field:"createTime",title:"创建时间",width:10},
    				{field:"updateBy",title:"更新者",width:15},
                    {field:"updateTime",title:"更新时间",width:15},
    				{field:"status",title:"权限状态",formatter:function(value,rowData,index){
    					if(value == 1){
    						return "可用";
    					}else if(value == 0){
    						return "禁用";
    					}else if(value == 2){
    						return "已删除";
    					}else{
    						return "";
    					}
    				}}
    				
    			]
                ],
    			loadFilter:function(data){
    				//data是服务器返回的数据,将服务器端返回的数据转换为datagrid需要的格式
    				return {"total":data.data.totalRows,"rows":data.data.result};
    			}
    		
    		});
    	});
    </script>
  </body>
</html>
